<template>
  <ex-dialog width="70%" class="ex-diolog" :title="title" :visible="visible" :close-on-click-modal="false"
    @closeDialog="handleClose">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm"
      v-loading="loading">
      <el-form-item label="区域名称：" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入区域名称" style="width:90%"></el-input>
      </el-form-item>
      <el-form-item label="版本名称：" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入区域名称" style="width:90%"></el-input>
      </el-form-item>
      <el-form-item label="升级内容：" prop="name">
        <el-input
          type="textarea"
          :rows="8"
          placeholder="请输入内容"
          v-model="textarea">
        </el-input>
      </el-form-item>
      <el-form-item label="上传安装包" >
          <el-upload class="upload-demo" :action="fileaction" :headers="headers" :data="fileData" :limit="1" 
            :on-success="handleFileChange" :show-file-list="true" :file-list="fileList">
            <el-button type="primary" size="small">上传安装包</el-button>
            <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
          </el-upload>
      </el-form-item>
      <el-form-item label="是否强制更新：" prop="sort">
        <el-switch
          v-model="ruleForm.value1"
          active-value="1" 
          inactive-value="2"
          active-text="是"
          inactive-text="否">
        </el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')" :loading="submitLoading" v-if="id==null">确 定</el-button>
      <el-button type="primary" @click="editForm('ruleForm')" :loading="submitLoading" v-if="id!=null">保 存</el-button>
    </span>
  </ex-dialog>
</template>
<script>
  import {
    getAreaSelect,
    addArea,
    getAreaDetail,
    editArea
  } from "@/api/setting";
  import Loading from "@/components/Loading/index";
  import Editor from '@/components/Editor';
  // api
  import {
    articleType,
    addArticle,
    editArticle,
    articleDetail
  } from "@/api/officialWebsite";
  import {
    getToken
  } from "@/utils/auth";
  export default {
    components: {
      Loading,
      Editor,
    },
    props: {
      visible: Boolean,
      id: String,
    },
    data() {
      return {
        title: '新建区域',
        loading: false,
        submitLoading: false,
        formInline: {
          name: "", //区域名称
        },
        title: '添加区域', //
        props: {
          multiple: true
        },
        fileList: [],
        fileaction: process.env.VUE_APP_BASE_API + "/common/v1/uploadSetFileName",
        headers: {
          "X-Access-Token": getToken(),
        }, //设置上传文件请求头
        ruleForm: {
          name: "", //区域名称
          areaList: [], //区域
          sort: "", //推荐序号
          value1: '1',
        },
        rules: {
          name: [{
            required: true,
            message: "请输入区域名称",
            trigger: ['blur', 'change']
          }, ],
          areaList: [{
            required: true,
            message: "请选择区域",
            trigger: "blur"
          }],
          sort: [{
            required: true,
            message: "请输入排序号",
            trigger: ['blur', 'change']
          }, ],
        },
        editId: '', //编辑id
      
      };
    },
    computed: {},
    created() {
      console.log(this.id)
      if (this.id != null) {
        this.title = '编辑区域'
        this.getInfo()
      }else{

      }
    },
    methods: {
      //取消
      handleClose(formName) {
        this.$emit("update:visible", false);
      },
      
      handleCityChange(array) {
        console.log(array)
      },
      getInfo() { //获取信息详情
        this.loading = true
        getAreaDetail(this.id).then((res) => {
            let info = res.result
            this.ruleForm.name = info.name
            this.ruleForm.sort = info.sort
            if (info.areaList.length > 0) {
              info.areaList.forEach(item => {
                var array = []
                array[0] = item.province
                array[1] = item.city
                array[2] = item.area
                this.ruleForm.areaList.push(array)
              })
              console.log('地区', this.ruleForm.areaList)
            }
            setTimeout(() => {
              this.loading = false;
            }, 800)
          })
          .finally(() => {
            setTimeout(() => {
              this.loading = false;
            }, 800)
          });
      },
       //导入文件
      handleFileChange(res, file) {
        console.log('文件', res, file)
        if (res.code == 200) {
          this.$message.success('导入成功');
          this.ruleForm.phoneList = res.result.url
        } else {
          this.$message.error(res.msg);
          this.fileList = []
          this.loading = false;
        }
      },
      submitForm(formName) {
        //创建确定
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.ruleForm.areaList.length > 0) {
              var areaList = []
              this.ruleForm.areaList.forEach(item => {
                var obj = {}
                obj.province = item[0]
                obj.city = item[1]
                obj.area = item[2]
                areaList.push(obj)
              })
            }
            var params = {
              name: this.ruleForm.name,
              sort: this.ruleForm.sort,
              areaList: areaList
            };
            this.submitLoading = true
            addArea(params).then((res) => {
                this.$message({
                  message: "提交成功",
                  type: "success",
                });
                setTimeout(() => {
                  this.$emit('success')
                  this.handleClose()
                  this.submitLoading = false
                }, 1000);
              })
              .finally(() => {
                setTimeout(() => {
                  this.submitLoading = false;
                }, 800)
              });

          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      editForm(formName) {
        //编辑确定
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.ruleForm.areaList.length > 0) {
              var areaList = []
              this.ruleForm.areaList.forEach(item => {
                var obj = {}
                obj.province = item[0]
                obj.city = item[1]
                obj.area = item[2]
                areaList.push(obj)
              })
            }
            var params = {
              name: this.ruleForm.name,
              sort: this.ruleForm.sort,
              areaList: areaList,
              id: this.id,
            };
            this.submitLoading = true
            editArea(params).then((res) => {
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                setTimeout(() => {
                  this.$emit('success')
                  this.handleClose()
                  this.submitLoading = false
                }, 1000);
              })
              .finally(() => {
                setTimeout(() => {
                  this.submitLoading = false;
                }, 800)
              });

          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
    },
  };

</script>
<style lang="scss" scoped>
  .ex-diolog {
    ::v-deep .ex--dialog--footer {
      border: none !important;
    }

    ::v-deep .ex--dialog--main {
      padding: 10px 20px !important;
    }
  }
.el-input{
  width: 240px !important;
}
</style>
